<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <link href="http://lyou.usts.top/lyou/webjars/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://lyou.usts.top/lyou/webjars/jquery/3.1.0/jquery.min.js"></script>
    <script src="http://lyou.usts.top/lyou/webjars/sockjs-client/1.0.0/sockjs.min.js"></script>
    <script src="http://lyou.usts.top/lyou/webjars/stomp-websocket/2.3.3/stomp.min.js"></script>

</head>
<body>
<noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websocket relies on Javascript being
    enabled. Please enable
    Javascript and reload this page!</h2></noscript>
<div id="main-content" class="container">
    <div class="row">
        <div class="col-md-6">
            <form class="form-inline">
                <div class="form-group">
                    <label for="connect">WebSocket connection:</label>
                    <button id="connect" class="btn btn-default" type="submit">Connect</button>
                    <button id="disconnect" class="btn btn-default" type="submit" disabled="disabled">Disconnect
                    </button>
                </div>
            </form>
        </div>
        <div class="col-md-6">
            <form class="form-inline">
                <div class="form-group">
                    <label for="name">What is your name?</label>
                    <input type="text" id="name" class="form-control" placeholder="Your name here...">
                </div>
                <button id="send" class="btn btn-default" type="submit">Send</button>
            </form>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <table id="conversation" class="table table-striped">
                <thead>
                <tr>
                    <th>Greetings</th>
                </tr>
                </thead>
                <tbody id="greetings">
                </tbody>
            </table>
        </div>
    </div>
</div>
<script>
    /**
     * Created by CJ on 2018-05-29.
     */
    var stompClient = null;

    function setConnected(connected) {
        $("#connect").prop("disabled", connected);
        $("#disconnect").prop("disabled", !connected);
        if (connected) {
            $("#conversation").show();
        }
        else {
            $("#conversation").hide();
        }
        $("#greetings").html("");
    }

    function connect() {
        var socket = new SockJS('http://lyou.usts.top:8080/lyou/lyou-websocket');
        stompClient = Stomp.over(socket);
        //默认参数
        var header = {
            id:'f8898d157b5c48f1b9491be514e27b4e',
            type:0,
            tag:0,
//          keyword:nu,
            range:200,
            lng:120.5761380000,
            lat:31.2518150000
        };
        stompClient.connect(header, function (frame) {
            setConnected(true);
            console.log('Connected: ' + frame);
            stompClient.subscribe('http://lyou.usts.top:8080/lyou/lyouws/topic/register', function (response) {
                console.log("--------"+response+"-  ------------");
                showGreeting("=="+JSON.parse(response.body).Authorization+"==");
                // showGreeting("=="+response.body+"==");
            });
        });
    }

    function disconnect() {
        if (stompClient !== null) {
            stompClient.disconnect();
        }
        setConnected(false);
        console.log("Disconnected");
    }

    function sendName() {
        //传入参数值
        stompClient.send("http://lyou.usts.top/lyou/ws/publish", {"name":"cj"}, JSON.stringify({'content': $("#name").val(),'id':'f8898d157b5c48f1b9491be514e27b4e',
            'title':'this webscoket title','type':0,'tag':0,"lat":33,"lng":33}));

    }

    function showGreeting(message) {
        //获取及时信息
        $("#greetings").append("<tr><td>" + message + "</td></tr>");

    }

    $(function () {
        $("form").on('submit', function (e) {
            e.preventDefault();
        });
        $( "#connect" ).click(function() { connect(); });
        $( "#disconnect" ).click(function() { disconnect(); });
        $( "#send" ).click(function() { sendName(); });
    });

</script>
</body>
</html>